//
//  HomePageController.m
//  qiuShiJiangHu
//
//  Created by xalo on 16/1/25.
//  Copyright © 2016年 糗事江湖项目组. All rights reserved.
//

#import "HomePageController.h"
#import "SegmentControlView.h"
#import "ArticleCell.h"
#import "VideoCell.h"
#import "PictureCell.h"
#import "HttpHelper.h"
@interface HomePageController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) SegmentControlView *segmentView;
@property (strong, nonatomic) IBOutlet UIScrollView *homeScrollView;
@property (strong, nonatomic) IBOutlet UITableView *articleTableview;
@property (strong, nonatomic) IBOutlet UITableView *videoTableView;
@property (strong, nonatomic) IBOutlet UITableView *pictureTableView;
@property (strong, nonatomic) IBOutlet UITableView *essenceTableView;
@property (nonatomic,strong) ArticleCell *articleCell;
@property (nonatomic,strong) VideoCell *videoCell;
@property (nonatomic,strong) PictureCell *pictureCell;



@end

@implementation HomePageController
//添加分段控制器
-(void)addSegmentForHomeController
{
    self.segmentView=[[SegmentControlView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    
    [self.view addSubview:self.segmentView];

}

//当scrollView滑动时和segment对应
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
 //计算当前的偏移量
    CGFloat offSet=scrollView.contentOffset.x;
    CGFloat width=scrollView.frame.size.width;
    NSInteger number=offSet/width;
    self.segmentView.segment.selectedSegmentIndex=number;
    
//    [self setData];

}
//当segment的index改变时和scrollView对应
-(void)changeSegmentIndex
{
  //获取当前segement的index
    NSInteger number=self.segmentView.segment.selectedSegmentIndex;
    //计算偏移量
    CGFloat offSet=self.homeScrollView.frame.size.width*number;
    [self.homeScrollView setContentOffset:CGPointMake(offSet, 0) animated:YES];
    
//    [self setData];
}

//加载数据
-(void)setData
{
    if (self.segmentView.segment.selectedSegmentIndex == 1) {
        
        [[HttpHelper sharedManager] fectchSourceWithUrl:kVideos block:^{
            
            [self.videoTableView reloadData];
            
        }];
    }else
    {
        [[HttpHelper sharedManager] fectchSourceWithUrl:kImages block:^{
            
            [self.pictureTableView reloadData];
            
        }];
        
    }
}


//设置代理
-(void)setDelegate
{
    self.homeScrollView.delegate=self;
    self.articleTableview.dataSource=self;
    self.articleTableview.delegate=self;
    self.videoTableView.dataSource=self;
    self.videoTableView.delegate=self;
    self.pictureTableView.dataSource=self;
    self.pictureTableView.delegate=self;
    self.essenceTableView.dataSource=self;
    self.essenceTableView.delegate=self;
}
//用xib创建单元格首先注册单元格
-(void)registTableViewCell
{
    [self.articleTableview registerNib:[UINib nibWithNibName:@"ArticleCell" bundle:nil] forCellReuseIdentifier:@"articleCell"];
    
    [self.videoTableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:@"videoCell"];
    
    [self.pictureTableView registerNib:[UINib nibWithNibName:@"PictureCell" bundle:nil] forCellReuseIdentifier:@"picCell"];
   
}

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar_background@2x"] forBarMetrics:(UIBarMetricsDefault)];
//    
//    [[HttpHelper sharedManager] fectchSourceWithUrl:kHomePage block:^{
//        
//        [self.articleTableview reloadData];
//        
//    }];
    
    [self setDelegate];


    
    [super viewDidLoad];
    [self addSegmentForHomeController];

    
    [self.segmentView.segment addTarget:self action:@selector(changeSegmentIndex) forControlEvents:UIControlEventValueChanged];
    


    
    [self.segmentView.segment addTarget:self action:@selector(changeSegmentIndex) forControlEvents:UIControlEventValueChanged];
    

    [self setDelegate];
    [self.segmentView.segment addTarget:self action:@selector(changeSegmentIndex) forControlEvents:UIControlEventValueChanged];


    [self registTableViewCell];
    
    
    
    // Do any additional setup after loading the view.
}
#pragma mark--dataSource必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==self.articleTableview) {
        
        return [[HttpHelper sharedManager] countOfModelArray];
        
    }else if (tableView==self.videoTableView){
        
        return [[HttpHelper sharedManager] countOfModelArray];
        
    }else if (tableView==self.pictureTableView){
        
        return [[HttpHelper sharedManager] countOfModelArray];
        
    }else{
        
        return [[HttpHelper sharedManager] countOfModelArray];
    }
    
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView==self.articleTableview) {
        
    self.articleCell=[tableView dequeueReusableCellWithIdentifier:@"articleCell" forIndexPath:indexPath];
        
        return self.articleCell;
    }else if (tableView==self.videoTableView){
        
        self.videoCell=[tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
        
        return self.videoCell;
    }else if (tableView==self.pictureTableView){
        
        self.pictureCell=[tableView
            dequeueReusableCellWithIdentifier:@"picCell" forIndexPath:indexPath];
        
        return self.pictureCell;
        
    }else{
        
        return self.pictureCell;
    }
//    self.videoCell=[tableView dequeueReusableCellWithIdentifier:@"videoCell" forIndexPath:indexPath];
//    return self.videoCell;
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230;
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
